# region imports
from AlgorithmImports import *
# endregion

class SwimmingGreenBull(QCAlgorithm):

    def initialize(self):

        # Set start date 
        self.set_start_date(2026, 3, 20)

        # Set cash
        self.set_cash(100000)

        # Add future
        future_object = self.add_future(ticker = Futures.Energy.CRUDE_OIL_WTI, resolution = Resolution.MINUTE, fill_forward = False, extended_market_hours = True)

        # Set filter
        future_object.set_filter(min_expiry_days = 0, max_expiry_days = 60)

        # Symbols list
        self.symbols_list = []

        # Total volume today
        self.total_volume_today = {}
    
    def on_securities_changed(self, changes):

        # Loop added securities
        for contract in changes.added_securities:

            # Check if it's not a continuous futures contract
            if "/" not in contract.symbol.value:

                # Store symbol
                self.symbols_list.append(contract.symbol)

                # Store in total volume today dictionary
                self.total_volume_today[contract.symbol] = 0
        
        # Loop removed securities
        for contract in changes.removed_securities:

            # Remove from symbols list
            self.symbols_list.remove(contract.symbol)

            # Remove from total volume today dictionary
            self.total_volume_today.pop(contract.symbol)

    def on_data(self, data: Slice):
        
        # Loop symbols list
        for symbol in self.symbols_list:

            # If contract is traded
            if data.bars.contains_key(symbol) == True:

                # Get tradebar
                trade_bar = data.bars[symbol]

                # Volume
                symbol_volume = trade_bar.volume

                # Store volume
                self.total_volume_today[symbol] += symbol_volume